
package cn.com.fattiger.framework.countdownlatch;

import java.util.concurrent.CountDownLatch;

/**
 * 
 * @author fattiger.xiaoyang
 * @date 2016/04/05
 */
public class CountDownLatchDemo {
	
	public static void main(String[] args){
		final int COUNT_NUM = 3;
		final CountDownLatch latch = new CountDownLatch(COUNT_NUM);
		for(int i=0; i<COUNT_NUM; i++){
			new Thread(new CountHandler(latch)).start();
		}
		try {
           System.out.println("等待3个子线程执行完毕...");
           latch.await();
           System.out.println("3个子线程已经执行完毕");
           new Thread(new Runnable(){
			@Override
			public void run() {
				System.out.println("继续执行子线程");
			}}).start();
//           System.out.println("继续执行主线程");
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
	}
}

class CountHandler implements Runnable{
	private CountDownLatch latch;//此定义接受构造的新值 无所赋值
	public CountHandler(CountDownLatch latch){
		this.latch = latch;
	}
	@Override
	public void run() {
		System.out.println("线程"+Thread.currentThread().getName()+"工作。");
		latch.countDown();
	}
}
